package com.javasm.service.goodsService.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.goodsCriteria.GdColourCriteria;
import com.javasm.domin.entity.goodsEntity.GdColour;
import com.javasm.domin.entity.goodsEntity.GdProduct;
import com.javasm.domin.vo.goodsVo.GdColourVo;
import com.javasm.mapper.goodsMapper.GdColourMapper;
import com.javasm.mapper.goodsMapper.GdProductMapper;
import com.javasm.service.goodsService.GdColourService;
import com.javasm.transfer.goodsTransfer.GdColourTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author Administrator
* @description 针对表【gd_colour】的数据库操作Service实现
* @createDate 2021-11-19 10:57:49
*/
@Service
@Transactional
public class GdColourServiceImpl extends ServiceImpl<GdColourMapper, GdColour>
implements GdColourService {

    @Autowired
    private GdColourMapper gdColourMapper;
    @Autowired
    private GdProductMapper gdProductMapper;

    @Autowired
    private GdColourTransfer gdColourTransfer;

    @Override
    public PageResult<GdColourVo> searchCriteria(GdColourCriteria gdColourCriteria) {
        String colourName = gdColourCriteria.getColourName();

        LambdaQueryWrapper<GdColour> lambda = new QueryWrapper<GdColour>().lambda();

        if (!StringUtils.isEmpty(colourName)){
            lambda.like(GdColour::getColourName,colourName);
        }

        PageHelper.startPage(gdColourCriteria.getCurrentPage(),gdColourCriteria.getPageSize());

        List<GdColour> gdColourList = gdColourMapper.selectList(lambda);

        PageInfo<GdColour> gdColourPageInfo = new PageInfo<>(gdColourList);

        long total = gdColourPageInfo.getTotal();

        List<GdColourVo> gdColourVos = gdColourTransfer.toVO(gdColourList);


        return new PageResult<GdColourVo>(total,gdColourVos);
    }

    @Override
    public int batchDeleteByIds(List<Long> ids) {

        List<Long> iii = null;

        int i1 =0;

        for (int i = 0; i < ids.size(); i++) {
            Long aLong = ids.get(i);
            i1 += selectColourId(aLong);

        }

        if (i1>0){
            return gdColourMapper.deleteBatchIds(iii);

        }else {
            return gdColourMapper.deleteBatchIds(ids);
        }

    }

    @Override
    public int selectColourId(Long id) {

        List<GdProduct> gdProducts = gdProductMapper.selectColourIdGdProducts(id);

        if (gdProducts.size()>0){
            return 1;
        }else {
            return 0;
        }

    }
}
